package com.example.task1.ui.AllService.Gover;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.GovernmentAppealSortBean;
import com.example.task1.bean.GovernmentBannerBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.youth.banner.Banner;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class GovernmentActivity extends BaseActivity {

    private Banner banner;
    private RecyclerView rvList;
    private Button myAppeal;
    private GovernmentSortAdapter governmentSortAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_government);
        initView();
        initData();
        setAdapter();
        getBanner();
        myAppeal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(GovernmentActivity.this, GovernmentMyAppealActivity.class);
            }
        });
    }

    private void getBanner() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/gov-service-hotline/ad-banner/list");
        Api.send(call, GovernmentBannerBean.class, new CallBack<GovernmentBannerBean>() {
            @Override
            public void success(GovernmentBannerBean data) {
                List<String> collect = data.getData().parallelStream().map(GovernmentBannerBean.DataDTO::getImgUrl).collect(Collectors.toList());
                MyApplication.banner(banner, collect);
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void setAdapter() {
        governmentSortAdapter = new GovernmentSortAdapter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        governmentSortAdapter.setHasStableIds(true);
        rvList.setAdapter(governmentSortAdapter);
        rvList.setLayoutManager(linearLayoutManager);
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/gov-service-hotline/appeal-category/list");
        Api.send(call, GovernmentAppealSortBean.class, new CallBack<GovernmentAppealSortBean>() {
            @Override
            public void success(GovernmentAppealSortBean data) {
                governmentSortAdapter.setData(data.getRows());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "政府热线";
    }

    private void initView() {
        banner = findViewById(R.id.banner);
        rvList = findViewById(R.id.rv_list);
        myAppeal = findViewById(R.id.my_appeal);
    }
}